package org.jeecg.modules.online.cgform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.online.cgform.entity.OnlCgformEnhanceJava;
import org.jeecg.modules.online.cgform.entity.OnlCgformEnhanceSql;
import org.jeecg.modules.online.cgform.mapper.OnlCgformEnhanceJavaMapper;
import org.jeecg.modules.online.cgform.mapper.OnlCgformEnhanceSqlMapper;
import org.jeecg.modules.online.cgform.service.IOnlCgformEnhanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service("onlCgformEnhanceService")
public class OnlCgformEnhanceServiceImpl implements IOnlCgformEnhanceService {

    @Autowired
    private OnlCgformEnhanceJavaMapper onlCgformEnhanceJavaMapper;

    @Autowired
    private OnlCgformEnhanceSqlMapper onlCgformEnhanceSqlMapper;

    @Override
    public List<OnlCgformEnhanceJava> queryEnhanceJavaList(String cgformId) {
        LambdaQueryWrapper<OnlCgformEnhanceJava> query = new LambdaQueryWrapper<OnlCgformEnhanceJava>()
                .eq(OnlCgformEnhanceJava::getCgformHeadId, cgformId);
        List<OnlCgformEnhanceJava> ls = onlCgformEnhanceJavaMapper.selectList(query);
        return ls;
    }

    @Override
    public void saveEnhanceJava(OnlCgformEnhanceJava onlCgformEnhanceJava) {
        onlCgformEnhanceJavaMapper.insert(onlCgformEnhanceJava);
    }

    @Override
    public void updateEnhanceJava(OnlCgformEnhanceJava onlCgformEnhanceJava) {
        onlCgformEnhanceJavaMapper.updateById(onlCgformEnhanceJava);
    }

    @Override
    public void deleteEnhanceJava(String id) {
        onlCgformEnhanceJavaMapper.deleteById(id);
    }

    @Override
    public void deleteBatchEnhanceJava(List<String> idList) {
        onlCgformEnhanceJavaMapper.deleteBatchIds(idList);
    }

    @Override
    public boolean checkOnlyEnhance(OnlCgformEnhanceJava onlCgformEnhanceJava) {
        LambdaQueryWrapper<OnlCgformEnhanceJava> query = new LambdaQueryWrapper<OnlCgformEnhanceJava>();
        query.eq(OnlCgformEnhanceJava::getButtonCode,onlCgformEnhanceJava.getButtonCode());
        query.eq(OnlCgformEnhanceJava::getCgformHeadId,onlCgformEnhanceJava.getCgformHeadId());
        //query.eq(OnlCgformEnhanceJava::getCgJavaType,onlCgformEnhanceJava.getCgJavaType());
        query.eq(OnlCgformEnhanceJava::getEvent,onlCgformEnhanceJava.getEvent());
        Integer count = this.onlCgformEnhanceJavaMapper.selectCount(query);
        if(count!=null) {
            if(count==1 && oConvertUtils.isEmpty(onlCgformEnhanceJava.getId())) {
                return false;
            }else if(count==2) {
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean checkOnlyEnhance(OnlCgformEnhanceSql onlCgformEnhanceSql) {
        LambdaQueryWrapper<OnlCgformEnhanceSql> query = new LambdaQueryWrapper<OnlCgformEnhanceSql>();
        query.eq(OnlCgformEnhanceSql::getButtonCode,onlCgformEnhanceSql.getButtonCode());
        query.eq(OnlCgformEnhanceSql::getCgformHeadId,onlCgformEnhanceSql.getCgformHeadId());
        Integer count = this.onlCgformEnhanceSqlMapper.selectCount(query);
        if(count!=null) {
            if(count==1 && oConvertUtils.isEmpty(onlCgformEnhanceSql.getId())) {
                return false;
            }else if(count>1) {
                return false;
            }
        }
        return true;
    }


    @Override
    public List<OnlCgformEnhanceSql> queryEnhanceSqlList(String cgformId) {
        LambdaQueryWrapper<OnlCgformEnhanceSql> query = new LambdaQueryWrapper<OnlCgformEnhanceSql>()
                .eq(OnlCgformEnhanceSql::getCgformHeadId, cgformId);
        List<OnlCgformEnhanceSql> ls = onlCgformEnhanceSqlMapper.selectList(query);
        return ls;
    }

    @Override
    public void saveEnhanceSql(OnlCgformEnhanceSql onlCgformEnhanceSql) {
        onlCgformEnhanceSqlMapper.insert(onlCgformEnhanceSql);
    }

    @Override
    public void updateEnhanceSql(OnlCgformEnhanceSql onlCgformEnhanceSql) {
        onlCgformEnhanceSqlMapper.updateById(onlCgformEnhanceSql);
    }

    @Override
    public void deleteEnhanceSql(String id) {
        onlCgformEnhanceSqlMapper.deleteById(id);
    }

    @Override
    public void deleteBatchEnhanceSql(List<String> idList) {
        onlCgformEnhanceSqlMapper.deleteBatchIds(idList);
    }
}
